package com.alan.wifishare.view.impl.activity;

import android.net.wifi.WifiInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alan.library.allog.KLog;
import com.alan.wifishare.R;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.app.UserConfig;
import com.alan.wifishare.model.business.ScanedWifiInfo;
import com.alan.wifishare.presenter.CNTConnectLinkPS;
import com.alan.wifishare.view.BaseActivity;
import com.alan.wifishare.view.impl.ActivityJumpUtil;
import com.alan.wifishare.view.iview.IVCNTConnectLink;
import com.alan.wifishare.view.ui.ALListViewDialog;
import com.alan.wifishare.view.ui.CNTDialogWifiScanAdapter;

import java.util.List;

/**
 * @className: CNTConnectLinkActivity
 * @describtion: CNTConnectLinkActivity
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/26 17:32
 */
public class CNTConnectLinkActivity extends BaseActivity implements IVCNTConnectLink, View.OnClickListener {

    private CNTConnectLinkPS cntConnectLinkPS = null;
    private Button cntConnectLinkBackBtn;
    private RelativeLayout cntConnectLinkSSIDRl;
    private TextView cntConnectLinkSSIDTv;
    private EditText cntConnectLinkPasswordEt;
    private Button cntConnectLinkSubmitBtn;
    private ALListViewDialog alListViewDialog;
    private CNTDialogWifiScanAdapter cntDialogWifiScanAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cnt_connect_link_layout);
        cntConnectLinkPS = new CNTConnectLinkPS(this);
        initView();
    }

    /**
     * 初始化界面
     */
    @Override
    protected void initView() {
        cntConnectLinkBackBtn = (Button)findViewById(R.id.cntConnectLinkBackBtn);
        cntConnectLinkSSIDRl = (RelativeLayout)findViewById(R.id.cntConnectLinkSSIDRl);
        cntConnectLinkSSIDTv = (TextView)findViewById(R.id.cntConnectLinkSSIDTv);
        cntConnectLinkPasswordEt = (EditText)findViewById(R.id.cntConnectLinkPasswordEt);
        cntConnectLinkSubmitBtn = (Button)findViewById(R.id.cntConnectLinkSubmitBtn);
        cntConnectLinkBackBtn.setOnClickListener(this);
        cntConnectLinkSSIDRl.setOnClickListener(this);
        cntConnectLinkSubmitBtn.setOnClickListener(this);
    }

    @Override
    public void showScanedWifiList(List<ScanedWifiInfo> scanedWifiInfoList) {
        if(cntDialogWifiScanAdapter != null){
            cntDialogWifiScanAdapter.setItemList(scanedWifiInfoList);
        }
    }

    @Override
    public String getLinkSSID() {
        return cntConnectLinkSSIDTv.getText().toString();
    }

    @Override
    public String getLinkPassword() {
        return cntConnectLinkPasswordEt.getText().toString();
    }

    @Override
    public void showProgress() {
        KLog.e();
        progressDialogShow();
    }

    @Override
    public void hideProgress() {
        KLog.e();
        progressDialogHiden();
    }

    @Override
    public void toastMsg(String msg) {
        toastMsgShort(msg);
    }

    @Override
    public void connectLinkSuccess() {
        // TODO: 连接成功返回到主界面
        WifiInfo curWifiInfo = GlobalField.wifiUtil.getWifiInfo();
        KLog.e("curWifiInfo = " + curWifiInfo);
        String SSID = curWifiInfo.getSSID();
        SSID = SSID.substring(1, SSID.length()-1);
        GlobalField.userConfig.setDeviceSSID(SSID);
        GlobalField.userConfig.setDevicePwd(getLinkPassword());
        GlobalField.userConfig.setDeviceMac(curWifiInfo.getBSSID());
        CNTConnectLinkActivity.this.setResult(ActivityJumpUtil.ResultCode.CNT_CONNECT_LINK_2_CNT_MAIN);
        CNTConnectLinkActivity.this.finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(alListViewDialog != null){
            if(alListViewDialog.isShowing()){
                alListViewDialog.dismiss();
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cntConnectLinkBackBtn:
                CNTConnectLinkActivity.this.finish();
                break;
            case R.id.cntConnectLinkSSIDRl:
                initWifiListDialog();
                cntConnectLinkPS.scanWifi();
                alListViewDialog.show();
                break;
            case R.id.cntConnectLinkSubmitBtn:
                cntConnectLinkPS.connectLink();
                break;
            default:
                break;
        }
    }


    private void initWifiListDialog(){
        if(alListViewDialog == null){
            alListViewDialog = new ALListViewDialog(CNTConnectLinkActivity.this, R.style.custom_dialog1);
            alListViewDialog.setCancelable(true);
            alListViewDialog.setTitleText("请选择要连接的设备：");
            alListViewDialog.setBottomBtnTextWithClickListener("刷新", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    cntConnectLinkPS.scanWifi();
                }
            });
            cntDialogWifiScanAdapter = new CNTDialogWifiScanAdapter(CNTConnectLinkActivity.this);
            alListViewDialog.setListAdapter(cntDialogWifiScanAdapter);
            alListViewDialog.setListOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    ScanedWifiInfo scanedWifiInfo = cntDialogWifiScanAdapter.getItemList().get(i);
                    cntConnectLinkSSIDTv.setText(scanedWifiInfo.getSSID());
                    alListViewDialog.dismiss();
                }
            });
        }
    }
}
